<?php
/**
 * This file is part of Task Agile.
 * Task Agile is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License.
 * Task Agile is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Task Agile.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category   TaskAgile
 * @package    Controller
 * @copyright  Copyright (c) 20010-2010 Estrada Virtual (http://estradavirtual.com.br)
 * @license    http://www.gnu.org/licenses/gpl.txt GPLv3
 * @version    $Id$
*/
/**
 * @filesource  23/09/2010
 * @author      Estrada Virtual <http://estradavirtual.com.br>
 * @copyright   Copyright <2010> Estrada Virtual
 * @package     <<application>>
 * @subpackage  <<application>>.application.models
 * @version     $Id$
 */
class TaskUpload extends Tev_Db_Table
{
    protected $_name = 'task_upload';
    protected $_referenceMap = array(
                    'Task' => array(
                            'columns'       => 'fk_task',
                            'refTableClass' => 'Task',
                            'refColumns'    => 'id',
                         ),
                    'User' => array(
                            'columns'       => 'fk_user',
                            'refTableClass' => 'User',
                            'refColumns'    => 'id',
                         ));

    /**
     * Upload file
     * @param int $idUser
     * @param int $idTask
     * @param string $fileName
     * @param string $tempFile
     * @return void
     */
    public function upload($idUser, $idTask, $fileName, $tempFile)
    {
        $pathDir = $this->getPath() . '/' . $idTask;
        if (!file_exists($pathDir)) {
            @mkdir($pathDir);
        }

        $targetFile = $pathDir . '/' . $fileName;
        move_uploaded_file($tempFile, $targetFile);

        if (!file_exists($targetFile)) {
            throw new Exception('Error upload file');
        }

        // Save on table
        $data = array('fk_user' => $idUser,
                      'fk_task' => $idTask,
                      'name'    => $fileName,
                      'dt_register' => date('Y-m-d h:i:s'));
        $row = $this->createRow();
        $row->setFromArray($data);
        $row->save();
    }

    public function findByTask($fkTask)
    {
        $select = $this->getAdapter()->select()
                       ->from(array('tu' => $this->_name))
                       ->join(array('u' => 'user'), 'u.id = tu.fk_user', array('user_name' => 'name'))
                       ->order('dt_register desc')
                       ->where('fk_task = ?', $fkTask);
       return $select->query()->fetchAll();
    }

    public function getPath()
    {
        return realpath(APPLICATION_PATH . '/data/upload/tasks');
    }

    public function getPathFile(Zend_Db_Table_Row $row)
    {
        $path = $this->getPath($row);
        $path .= '/' . $row->fk_task;
        $path .= '/' . $row->name;
        return $path;
    }
}
